from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
import dashscope

dashscope.api_key="sk-c173650c330c4cf0bbe36b8b0fbc3183"
prompt = '迷雾里的宇宙战舰，充满神秘感'

response = dashscope.ImageSynthesis.call(model='wanx-v1',
                                    prompt=prompt,
                                    n=1,
                                    size='1024*1024')

if response.status_code == HTTPStatus.OK:
    # save file to current directory
    for result in response.output.results:
        file_name = PurePosixPath(unquote(urlparse(result.url).path)).parts[-1]
        print(result.url)
        print(file_name)
        with open('./%s' % file_name, 'wb+') as f:
            f.write(requests.get(result.url).content)
else:
    print('Failed, status_code: %s, code: %s, message: %s' %
            (response.status_code, response.code, response.message))
